package com.example.yunhailutong.zhongtan.Adapter;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;

import com.example.yunhailutong.zhongtan.R;
import com.example.yunhailutong.zhongtan.activity.MessageDetailsActivity;
import com.example.yunhailutong.zhongtan.entity.HomeEntity.DataBean;
import com.example.yunhailutong.zhongtan.entity.HomeEntity.DataBean.HomeInfoBean;

import java.util.List;


public class ThirdPartViewPagerAdapter extends PagerAdapter {
    private List<HomeInfoBean> lstBgtBeanList;
    private List<HomeInfoBean> lstFlyzBeans;
    private List<HomeInfoBean> lstDxfcBeans;
    private List<HomeInfoBean> lstDyfcBeans;
    private ThirdPartListViewAdapter listViewAdapter;
    private DataBean mDataBean;

    public ThirdPartViewPagerAdapter(DataBean datalist) {
        mDataBean = datalist;
    }
    @Override
    public int getCount() {
        return 4;
    }

    @Override
    public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
        return view == object;
    }

    @Override
    public Object instantiateItem(@NonNull ViewGroup container, int position) {
        //super.instantiateItem(container, position);

        final int view_index = position;
        View view = View.inflate(container.getContext(), R.layout.fragment_main_third_part, null);
        ListView listView = view.findViewById(R.id.third_part_list_view);
        switch (position) {
            case 0:
                lstDxfcBeans = mDataBean.getLstDxfc();
                listViewAdapter = new ThirdPartListViewAdapter(container.getContext(), lstDxfcBeans);
            break;
            case 1:
                lstFlyzBeans = mDataBean.getLstFlyz();
                listViewAdapter = new ThirdPartListViewAdapter(container.getContext(), lstFlyzBeans);
                break;
            case 2:
                lstDyfcBeans = mDataBean.getLstDyfc();
                listViewAdapter = new ThirdPartListViewAdapter(container.getContext(), lstDyfcBeans);
                break;
            case 3:
                lstBgtBeanList = mDataBean.getLstBgt();
                listViewAdapter = new ThirdPartListViewAdapter(container.getContext(), lstBgtBeanList);
                break;
                default:
        }
        if (!listViewAdapter.isEmpty()) {
            listView.setAdapter(listViewAdapter);
        }

        //ListView Item 监听点击事件
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                HomeInfoBean informationBean = null;
                switch (view_index) {
                    case 0:
                        lstDxfcBeans = mDataBean.getLstDxfc();
                        informationBean = lstDxfcBeans.get(position);
                        break;
                    case 1:
                        lstFlyzBeans = mDataBean.getLstFlyz();
                        informationBean = lstFlyzBeans.get(position);
                        break;
                    case 2:
                        lstDyfcBeans = mDataBean.getLstDyfc();
                        informationBean = lstDyfcBeans.get(position);
                        break;
                    case 3:
                        lstBgtBeanList = mDataBean.getLstBgt();
                        informationBean = lstBgtBeanList.get(position);
                        break;
                    default:

                }
                Intent intent = new Intent(view.getContext(), MessageDetailsActivity.class);
                Bundle bundle = new Bundle();
                bundle.putString("content", informationBean.getIm_content());
                bundle.putString("title", informationBean.getIm_title());
                bundle.putString("img_url", informationBean.getIm_imgPath());
                intent.putExtras(bundle);
                view.getContext().startActivity(intent);
            }
        });

        container.addView(view);
        return view;
    }

    @Override
    public int getItemPosition(@NonNull Object object) {
        return super.getItemPosition(object);
    }

    @Override
    public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
        //super.destroyItem(container, position, object);
        container.removeView((View)object);
    }
}
